﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;

namespace OAWeb
{
    public partial class m_Meeting : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          if(!IsPostBack)
          {
              ////AddListItems(lbMaster,"select AutoID,UserName from usersV where EnterpriseID = " +
              //                                Session["EnterpriseID"]);
              ////AddListItems(lbRecorder, "select AutoID,UserName from usersV where EnterpriseID = " +
              //                                Session["EnterpriseID"]);
              ////AddListItems(lbJoiner, "select AutoID,UserName from usersV where EnterpriseID = " +
              //                                Session["EnterpriseID"]);
              AddHoursDropDownList(ddlStartHour);
              AddHoursDropDownList(ddlEndHour);
          }
           
        }
        protected void OnUsersStrChanged(object sender, EventArgs e)
        {
            //添加接收人
            if (SelUser1.UsersStrID != null)
            {
                if(SelUser1.Tag=="ddlMaster")
                {
                    strSQL = "select AutoID,UserName from Users where AutoID in(" + SelUser1.UsersStrID + ")";
                    conn.Fill(strSQL, null);
                    ddlMaster.DataValueField = "AutoID";
                    ddlMaster.DataTextField = "UserName";
                    ddlMaster.DataSource = conn.ds;
                    ddlMaster.DataBind();
                }
                if (SelUser1.Tag == "ddlRecorder")
                {
                    strSQL = "select AutoID,UserName from Users where AutoID in(" + SelUser1.UsersStrID + ")";
                    conn.Fill(strSQL, null);
                    ddlRecorder.DataValueField = "AutoID";
                    ddlRecorder.DataTextField = "UserName";
                    ddlRecorder.DataSource = conn.ds;
                    ddlRecorder.DataBind();
                }
                if (SelUser1.Tag == "ddlJoiner")
                {
                    strSQL = "select AutoID,UserName from Users where AutoID in(" + SelUser1.UsersStrID + ")";
                    conn.Fill(strSQL, null);
                    ddlJoiner.DataValueField = "AutoID";
                    ddlJoiner.DataTextField = "UserName";
                    ddlJoiner.DataSource = conn.ds;
                    ddlJoiner.DataBind();
                }
            }
           
        }
        //protected void OnUsersStrChanged2(object sender, EventArgs e)
        //{
        //    //添加接收人
        //    if (SelUser2.UsersStrID != null)
        //    {
        //        if (SelUser2.Tag == "ddlRecorder")
        //        {
        //            strSQL = "select AutoID,UserName from Users where AutoID in(" + SelUser2.UsersStrID + ")";
        //            conn.Fill(strSQL, null);
        //            ddlRecorder.DataValueField = "AutoID";
        //            ddlRecorder.DataTextField = "UserName";
        //            ddlRecorder.DataSource = conn.ds;
        //            ddlRecorder.DataBind();
        //        }

        //    }
        //}
 
        public void AddListItems(ListBox ddl,string sql)
        {
            SqlDataReader dr = myDataReader(sql);
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Text = dr[1].ToString();
                li.Value = dr[0].ToString();
                ddl.Items.Add(li);
            }
        }
        public string reStr(string str)
        {
            return str.Replace('\'', ' ');
        }

        public SqlDataReader myDataReader(string sql)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            try
            {
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;
            }
            catch (Exception )
            {

                return null;
            }
            
            
        }
        public bool myExecNonQuery(string sql)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        public void AddHoursDropDownList(DropDownList ddl)
        {
            for (int i=1;i<=24;i++)
            {
                ListItem li=new ListItem();
                li.Text = i + "点";
                li.Value = i.ToString();
                ddl.Items.Add(li);
            }
        }
        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
                try
                {
                    string fileName = FileUpload1.FileName;
                    string _fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +
                                       DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "-" + fileName;
                    FileUpload1.SaveAs(HttpContext.Current.Request.MapPath("~/uploadfile/") + _fileName);
                    myExecNonQuery("insert into m_uploadfile (mID,filename,newfilename)values(0,'" + reStr(fileName) + "','" +
                                 _fileName + "')");
                    SqlDataReader dr = myDataReader("select top 1 id,filename from m_uploadfile order by id desc");
                    if(dr.Read())
                    {
                        ListItem li=new ListItem();
                        li.Text = dr[1].ToString();
                        li.Value = dr[0].ToString();
                        ddlUploadFileStr.Items.Add(li);
                    }
                    Panel2.Visible = false;
                }
                catch (Exception ex)
                {
                    //Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                Panel2.Visible = false;
            }
        }

        protected void btnFuJian_Click(object sender, EventArgs e)
        {
            Panel2.Visible = true;
        }
 
        protected void BtnSend_Click(object sender, EventArgs e)
        {
            string mMasters="|", mRecorders="|", mJoiners="|";
            foreach (ListItem li in ddlMaster.Items)
            {
                mMasters = Master + li.Text +"|";
            }
            foreach (ListItem li in ddlRecorder.Items)
            {
                mRecorders = mRecorders + li.Text + "|";
            }
            foreach (ListItem li in ddlJoiner.Items)
            {
                mJoiners = mJoiners + li.Text + "|";
            }

            string sql = "insert into m_meetinginfo (EnterpriseID,mTitle,mLevel,mMoney,mContent,mMasterID,mRoomID,mInTime,mOutTime,mMasters,mRecorders,mJoiners)values";
            sql = sql + "(" + Session["EnterpriseID"] + ",'" + reStr(tbTitle.Text) + "','" + ddlLevel.SelectedValue + "'," +
                  reStr(tbMoney.Text) + ",'" + reStr(FCKeditor2.Value) + "'," + Session["UserID"] + "," + ddlRoom.SelectedValue + ",'" +
                  tbStartDate.Text + " " + ddlStartHour.SelectedValue + ":" + tbSecend1.Text + ":00','" +
                  tbStartDate.Text + " " + ddlEndHour.SelectedValue + ":" + tbSecend2.Text + ":00','" + mMasters + "','" +
                  mRecorders + "','" + mJoiners + "')";
            //Response.Write(sql);
            myExecNonQuery(sql);
            SqlDataReader dr = myDataReader("select top 1 mID from m_meetinginfo order by mid desc");
            if(dr.Read())
            {
                string mID = dr[0].ToString();
                foreach(ListItem li in ddlMaster.Items)
                {
                    myExecNonQuery("insert into m_Joiners (mID,uID,isType,isStatues)values(" + mID + "," +
                                   li.Value + ",1,0)");
                }
                foreach (ListItem li in ddlRecorder.Items)
                {
                    myExecNonQuery("insert into m_Joiners (mID,uID,isType,isStatues)values(" + mID + "," +
                                   li.Value + ",2,0)");
                }
                foreach (ListItem li in ddlJoiner.Items)
                {
                    myExecNonQuery("insert into m_Joiners (mID,uID,isType,isStatues)values(" + mID + "," +
                                   li.Value + ",3,0)");
                }
                foreach (ListItem li in ddlUploadFileStr.Items)
                {
                    myExecNonQuery("update m_uploadfile set mID = " + mID + " where ID = " + li.Value);
                }
                //Literal1.Text = "<script language=javascript>alert('会议发起成功！');</script>";
                //Literal1.Text = "<script language=javascript>alert('会议发起成功！');</script>";
                //Response.Redirect("~/m_mymeeting.aspx");
                Session["ShowMsg"] = "发送成功!";
                Response.Redirect("ShowMessage.aspx?PreviousPage=m_meeting.aspx");

            }
            else
            {
                Literal1.Text = "<script language=javascript>alert('会议发起失败，请检查是否有遗漏项目未填写！');</script>";
                Response.Redirect("~/m_meeting.aspx");
            }
            
        }

        protected void btnMeetingRoom_Click(object sender, EventArgs e)
        {
            ddlRoom.Items.Clear();
            string sTime = tbStartDate.Text + " " + ddlStartHour.SelectedValue + ":" + tbSecend1.Text + ":00";
            string sql = "select mroomID from m_MeetingInfo where ('" + sTime +
                         "'  between mInTime and mOutTime) and (EnterpriseID =" + Session["EnterpriseID"] +
                         " )  group by mroomid";
            string _sql = "select rID,RoomName from m_roominfo where (rid not in ("+ sql +"))";

            SqlDataReader dr = myDataReader(_sql);
            
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Text = dr[1].ToString();
                li.Value = dr[0].ToString();
                ddlRoom.Items.Add(li);
            }
        }
        protected void btnDelFile_Click(object sender, EventArgs e)
        {
            SqlDataReader dr = myDataReader("select newfilename from m_uploadfile where id ="+ddlUploadFileStr.SelectedValue);
            if(dr.Read())
            {
                string fn = HttpContext.Current.Request.MapPath("~/uploadfile/") + dr[0];
                File.Delete(fn);
            }
            ddlUploadFileStr.Items.Remove(ddlUploadFileStr.SelectedItem);
        }

    }
}
